using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace de_kleintjesmarkt
{
    public class ChallengeBuilder
    {
        public static string createMD5( string _password )
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            System.Security.Cryptography.MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            originalBytes = System.Text.ASCIIEncoding.Default.GetBytes(_password);
            encodedBytes = md5.ComputeHash(originalBytes);

            return md5ToString( encodedBytes );
        }

        public static string md5ToString(Byte[] _md5)
        {
            string returnValue;

            //Convert encoded bytes back to a 'readable' string
            returnValue = BitConverter.ToString( _md5 );
            
            return stripMD5(returnValue);
        }

        public static string stripMD5(string _md5)
        {
            string returnValue;

            returnValue = _md5.Replace("-", "");
            returnValue = returnValue.ToLower();

            return returnValue;
        }
    }
}
